package com.microcm.system.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.microcm.common.SystemToolkit;
import com.microcmframework.core.BaseServlet;
import com.microcmframework.core.cache.ICacheFace;

/**
 * 用于刷新系统缓存
 * 
 * @author kansum_li
 * @date: 2011-4-30
 */
public class CacheReflash extends BaseServlet
{
	private static final long serialVersionUID = 1L;

	private Map<String, ICacheFace> cacheFaceList;

	@Override
	public void init(ServletConfig cfg) throws ServletException
	{
		super.init(cfg);
		cacheFaceList = new HashMap<String, ICacheFace>();
	}

	@Override
	protected void doProc(PrintWriter out, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// 设置字符集
		String _cacheCodes = req.getParameter("cacheCodes");
		if (_cacheCodes == null)
			resp.getWriter().print("{'status':'error','values':'cache name invalid!'}");
		else
		{
			String[] cacheCodes = _cacheCodes.split(",");
			for (String cacheCode : cacheCodes)
			{
				ICacheFace cacheFace = null;
				if (cacheFaceList.containsKey(cacheCode))
					cacheFace = cacheFaceList.get(cacheCode);
				else
				{
					cacheFace = (ICacheFace) SystemToolkit.getBean(cacheCode);
					if (cacheFace == null)
					{
						resp.getWriter().print("{status:'error',values:'not found cache bean(" + cacheCode + ")!'}");
						return;
					}
					else
						cacheFaceList.put(cacheCode, cacheFace);
				}
				cacheFace.refresh();
			}
			out.print("{'status':'success','values':'ok!'}");
		}
	}

}